//
//  ObjcController.swift
//  SwiftDemo
//
//  Created by lee on 2018/8/21.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

import UIKit

class ObjcController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    func fetchPropertyList(_ obj: Any?) {
        var count:UInt32 = 0;
        
        guard let propertyList = class_copyPropertyList(object_getClass(obj), &count)  else {
            print("propertyList == nil");
            return;
        }
        
        for i in 0..<Int(count) {
            //            let property = propertyList[i];
            //            let cpropertyName = String(utf8String: property);
            //            let propertyName = String(utf8String: cpropertyName);
            //
            //            guard let cpropertyAttr = property_getAttributes(property) else {
            //                return;
            //            }
            //            let propertyArr = String(utf8String: cpropertyAttr)
            //
            //            print("propertyName=\(propertyName), propertyArr=\(propertyArr)")
            
        }
        free(propertyList);
    }
    
}

/* public open 跨模块访问
 internel
 private fileprivate
 */

///  method swizzle 调用
extension ObjcController: AwakeProtocol {
    static func awake() {
        print("replace depreted method load");
        
        guard let m1 = class_getInstanceMethod(self, #selector(ms_viewWillAppear(_:))) else {
            return;
        }
        guard let m2 = class_getInstanceMethod(self, #selector(viewWillAppear(_:))) else {
            return;
        }
        
        if (class_addMethod(self, #selector(viewWillAppear(_:)), method_getImplementation(m2), method_getTypeEncoding(m2))) {
            class_replaceMethod(self, #selector(ms_viewWillAppear(_:)), method_getImplementation(m1), method_getTypeEncoding(m1));
        } else {
            method_exchangeImplementations(m1, m2);
        }
    }
    
    @objc func ms_viewWillAppear(_ animated:Bool) {
        ms_viewWillAppear(animated);
        print("ms_viewWillAppear");
    }
    
}
